﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


namespace UIFramwork
{
    /// <summary>
    /// 图形界面|管理面板
    /// </summary>
    public class UIMgr : MonoSingletonBase<UIMgr>
    {
        public IEnumerator Start()
        {
            this.m_UIQueue = new Queue<Action>();
            WaitForSeconds wait1f = new(0.1f);
            while (true)
            {
                if (this.m_UIQueue.Count != 0)
                {
                    this.m_UIQueue.Dequeue()?.Invoke();

                    yield return null;
                    continue;
                }

                yield return wait1f;
            }
        }


        public event EventHandler<UIEventArgs> OnUIUpdated;
        public event EventHandler<UIEventArgs> OnUIRemoved;



        [Tooltip("加载队列项")] Queue<Action> m_UIQueue;
        [Tooltip("图像在用库")] UIAssetsUsed m_AssetsUsed;
        [Tooltip("UI相机项"), HideInInspector] public Camera UICamera;

        [Tooltip("画布")] public static Canvas Canvas;



        /// <summary>
        /// 初始化|UI系统
        /// </summary>
        public static void Init()
        {
            Canvas = Instance.transform.GetComponent<Canvas>();

            // 激活 在用库
            Instance.m_AssetsUsed = new UIAssetsUsed(Instance);

            // 关联 UI Camera
            Instance.UICamera = Instance.transform.GetComponent<Canvas>().worldCamera;
        }


        /// <summary>
        /// 访问|页面（立即）
        /// </summary>
        public static T Open<T>() where T : UIBase
        {
            Type type = typeof(T);

            bool isGet = Instance.m_AssetsUsed.TryGet(type, out UIBase output);
            if (!isGet)
            {
                output = Instance.m_AssetsUsed.DoCreate(type);
            }

            Instance.m_AssetsUsed.DoUse(type, output);
            return output as T;
        }

        /// <summary>
        /// 访问|页面（队列）
        /// </summary>
        public static T OpenQueue<T>() where T : UIBase
        {
            Type type = typeof(T);

            bool isGet = Instance.m_AssetsUsed.TryGet(type, out UIBase output);
            if (!isGet)
            {
                output = Instance.m_AssetsUsed.DoCreate(type);
            }

            UIEventArgs eventArgs = new(type, output);

            // 获取组件
            Instance.m_UIQueue.Enqueue(() => Instance.OnUIUpdated?.Invoke(null, eventArgs));
            return output as T;
        }


        /// <summary>
        /// 关闭|页面（立即）
        /// </summary>
        public static void Close<T>(UIBase uiBase = null) where T : UIBase
        {
            Type type = typeof(T);
            Instance.m_AssetsUsed.DoRemove(type, uiBase);
        }

        /// <summary>
        /// 关闭|页面（队列）
        /// </summary>
        public static void CloseQueue<T>(UIBase uiBase = null) where T : UIBase
        {
            Type type = typeof(T);
            UIEventArgs eventArgs = new UIEventArgs(type, uiBase);

            Instance.m_UIQueue.Enqueue(() => Instance.OnUIRemoved.Invoke(null, eventArgs));
        }
    }
}